from selenium import webdriver
from selenium.webdriver.common.by import By

# 定义一个类，封装所有的页面操作
class PageLogin:
    def __init__(self): # 初始化操作
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.driver.get('http://127.0.0.1:8000/admin/')
    def page_input_username(self, username): # 输入用户名
        el = self.driver.find_element(By.CSS_SELECTOR, '#id_username')
        el.clear() # 先清空文本框，再输入新的
        el.send_keys(username)
    def page_input_password(self, password): # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, '#id_password').send_keys(password)
    def page_click_login_btn(self): # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
    def page_login(self, username, password): # 组装登录功能
        self.page_input_username(username)
        self.page_input_password(password)
        self.page_click_login_btn()
    def page_get_result(self): # 获取登录结果
        return self.driver.find_element(By.CSS_SELECTOR, '.errornote').text
    def page_close(self): # 关闭浏览器驱动
        self.driver.quit()
if __name__ == '__main__':
    login = PageLogin()
    login.page_login('admin', '888999')
    result = login.page_get_result()
    print('结果：', result)
    from time import sleep
    sleep(3)
    login.page_close()